//tslint:disable
import { Component, Input, OnInit } from '@angular/core';
import { AppGlobalService } from '../../shared/service/app-global.service';
import { PageModalService } from '../../shared/service/page-modal-service';
import { HttpService } from '../../shared/service/http.service';
import { MyBiconModalComponent } from '../../coms/my-bicon-modal/my-bicon-modal.component';
import { ModalController } from '@ionic/angular';
import { SharePageComponent } from '../share-page/share-page.component';
import { EventService } from 'src/app/shared/service/event.service';
@Component({
  selector: 'app-my-bicon-links-style',
  templateUrl: './my-bicon-links-style.component.html',
  styleUrls: ['./my-bicon-links-style.component.scss'],
})
export class MyBiconLinksStyleComponent implements OnInit {
  @Input() tagData;
  index: any;
  tabBarUnderlineStyle: any;
  tabBarTextStyle: any;
  titleBarStyle: any;
  tabBarBackGroundColor = '#FFFFFF';
  tabTitleSize = "100";
  listData: any;
  tabLength = 1;

  showIconLimit = 4; // 默认显示4个按钮
  isExpend = false; // 默认收起
  expendIconId = 'expendIconId';
  constructor(
    private appGlobalService: AppGlobalService,
    private pageModalService: PageModalService,
    private modalController: ModalController,
    private httpService: HttpService,
    private eventService: EventService,
  ) { }

  ngOnInit() {

    let len = 1;
    if (this.tagData.data.data && this.tagData.data.data.tab) {
      len = this.tagData.data.data.tab.length;
      this.tabLength = this.tagData.data.data.tab.length - 1;
    }
    // console.log(this.tagData.data.data.titleLeft);
    this.tabBarUnderlineStyle = {
      ['border-radius']: '0.31rem 0.31rem 0.31rem 0.31rem',
      width: (100 / len) + '%',
      height: '2rem',
      border: 'unset',
      background: 'rgba(206, 210, 217, 0.3)'
    };
    this.tabTitleSize = undefined;
    this.tabBarTextStyle = {
      // ['font-size']: '0.75rem',
      // color: '#7E838C',
      // ['font-weight']: 'normal',
      height: '2rem',
      // ['border-radius']: '0rem 0rem 0rem 0rem',
      // ['border-left']: '0.03rem solid #ced2d9'
    };

    this.titleBarStyle =
      'width:' + (100 / len) + 'vw;' +
      'height: 2rem;' +
      'display: flex;' +
      'color: #7E838C;' +
      'font-weight: normal;' +
      'font-size: 0.75rem;' +
      'align-items: center;' +
      'justify-content:center;' +
      'border-bottom: 0.03rem solid #ced2d9;' +
      'border-right: 0.03rem solid #ced2d9;' +
      'border-top: 0.03rem solid #ced2d9;'




    // ,
    // ['border']: '0.03rem solid #ced2d9'
    // };
    this.index = 0;

    // 蜂喔商户1.6.33 新增可配置的图标个数
    if (this.tagData && this.tagData.data && this.tagData.data.data.data.data) {
      console.log('showIconLimit2=' + this.tagData.data.data.data.data.length);
      this.showIconLimit = this.tagData.data.data.data.data.length;
      this.expendIconId += new Date().getTime();
      if (this.tagData.data.data.data.link && this.tagData.data.data.data.link.params && this.tagData.data.data.data.link.params.iconLimitCount) {
        this.showIconLimit = this.tagData.data.data.data.link.params.iconLimitCount;
      }
    }


    if (this.tagData && this.tagData.data && this.tagData.data.data) {
      if (this.tagData.data.data.titleBarStyle) {
        this.titleBarStyle = this.tagData.data.data.titleBarStyle;
      }

    }

    if (this.tagData.data.data && this.tagData.data.data.checkTabIndex !== undefined) {
      setTimeout(() => {
        document.getElementById('switchBoxTabPane' + this.tagData.data.data.checkTabIndex).click();
        if (this.tagData.data.data.checkTabIndex !== '0') {
          this.tabClick('', this.tagData.data.data.checkTabIndex, this.tagData.data.data);
        }
      }, 0);
    }

  }

  openPage(item) {
    try {
      if (item.submit) {
        const params = {
          content: item.submit.content,
          id: item.submit.id,
          sessionId: localStorage.getItem("sessionId")
        };
        this.httpService.post("application/submit", params, res => {
          if (res.success) {
            if (item.link) {
              this.pageModalService.openPage(item.link);
            }
          }
        })
      } else if (item.link) {
        this.pageModalService.openPage(item.link);
      }
    } catch (error) {
    }
  }

  openModal(item) {
    if (item && item.tip) {
      this.appGlobalService.showToast(item.tip ? item.tip : '正在快马加鞭开发中', 2000, "middle");

      return;
    }
    if (item && item.linkPageAfterSubmitSuccess) {
      const params = {
        content: item.submit.content,
        id: item.submit.id,
        sessionId: localStorage.getItem("sessionId")
      };
      this.httpService.post("application/submit", params, res => {
        if (res.success) {
          if (res.target !== null) {
            let linkType = 'page';
            const pageParams = {
              type: linkType,
              pageId: res.target,
              params: {},
              sessionId: localStorage.getItem('sessionId')
            };

            if (res.skip && res.skip === 4) { // modal
              linkType = 'pageModal';
              pageParams.type = linkType;
              pageParams['target'] = res.target;
              pageParams.params = res.params;
              this.pageModalService.openPage(pageParams);
            } else if (res.skip && res.skip === 1) { // page
              linkType = 'page';
              pageParams.type = linkType;
              pageParams['target'] = res.target;
              pageParams.params = res.params;
              this.pageModalService.openPage(pageParams);
            } else if (res.skip && res.skip === 2) { // h5
              linkType = 'url';
              pageParams.params = res.params;
              pageParams.params['urlLinkTitle'] = item.link.params.title;
              pageParams['target'] = res.target;
              pageParams.type = linkType;
              this.pageModalService.openPage(pageParams);
            } else if (res.skip && res.skip === 3) { // download
              const href = res.target;
              window.open(href, '_blank'); // _self
            }
          } else if (item.link) {
            this.pageModalService.openPage(item.link);
          }
        }
        if (res.message && res.message.length > 0) {
          this.appGlobalService.showToast(res.message, 1000, "top");
        }

      });
    } else {
      if (item && item.link && item.link.target) {
        item.count = 0;
        if (item.link.params && item.link.params.showName) {
          sessionStorage.setItem('showName', item.link.params.showName); // 我的业务点击
        }
        this.pageModalService.openPage(item.link);

      }
    }


  }
  async openMyBiconModal(className, item: any) {
    try {
      const params = {
        sessionId: localStorage.getItem("sessionId"),
        pageId: item.link.target,
        params: item.link.params
      };
      this.httpService.post('application/page', params, async res => {
        const tagData = JSON.parse(res.script).views[0].tags[0].data;
        const modal = await this.modalController.create({
          showBackdrop: true,
          cssClass: className,
          animated: false,
          component: MyBiconModalComponent,
          componentProps: { tagData }
        });
        await modal.present();
        // 监听销毁的事件
        const modalData = await modal.onDidDismiss(); // 获取关闭传回的值
      });
    } catch (error) {
      console.log('error----->' + JSON.stringify(error));
    }

  }


  openShareModal(link) {
    if (link) {

      const params = {
        sessionId: localStorage.getItem("sessionId"),
        pageId: link.target,
        params: link.params
      };
      this.httpService.post('application/page', params, res => {
        console.log(JSON.parse(res.script));
        const tmpShareData = JSON.parse(res.script).views[0].tags[0].data;
        this.presentShareModal(tmpShareData);
      });
    }
  }


  // 分享弹框
  async presentShareModal(shareData) {
    const modal = await this.modalController.create({
      showBackdrop: true,
      cssClass: 'sharePage',
      backdropDismiss: true,
      component: SharePageComponent,
      componentProps: { tagData: shareData }
    });
    await modal.present();
    // 监听销毁的事件
    const modalData = await modal.onDidDismiss(); // 获取关闭传回的值
    console.log(modalData);
  }

  onChange(item) {
    const that = this;
    try {
      this.index = item.index;
    } catch (error) {
      console.log('msg----->' + JSON.stringify(error));
    }
  }

  detailPage(link) {
    if (!link) {
      return;
    }
    const pageParams = {
      pageId: link.target,
      params: link.params,
      sessionId: localStorage.getItem('sessionId')
    };
    this.appGlobalService.goToForward(pageParams);
  }


  // 展开或者收起
  expend() {
    this.isExpend = !this.isExpend;
    const imgIcon = document.getElementById(this.expendIconId) as HTMLImageElement;
    if (imgIcon) {
      if (this.isExpend) {
        if (this.tagData.data && this.tagData.data.data.data.link && this.tagData.data.data.data.link.params && this.tagData.data.data.data.link.params.closeImagUrl) {
          imgIcon.src = this.tagData.data.data.data.link.params.closeImagUrl;
        }
        this.showIconLimit = this.tagData.data.data.data.data.length;
        // 收起
      } else {
        if (this.tagData.data && this.tagData.data.data.data.link && this.tagData.data.data.data.link.params && this.tagData.data.data.data.link.params.openImagUrl) {
          imgIcon.src = this.tagData.data.data.data.link.params.openImagUrl;
        }
        this.showIconLimit = this.tagData.data.data.data.link.params.iconLimitCount;
        // 展开
      }
    }
  }
  // 按钮点击事件
  tabClick(event, id, item) {
    if (event) {
      event.stopPropagation();
    }
    document.getElementById('switchBoxTabPane' + id).click();
    item.checkTabIndex = id;
    if(item.tab && item.tab[id] && item.filter) {
      const listTagName = item.tagName + 'AskForList';
      let param: any = {};
      const filter: any = {};
      const obj = JSON.parse(JSON.stringify(item.tab[id]));
      if(obj.time && obj.endTime) {
        filter[item.filter] = `${obj.time},${obj.endTime}`;
      } else if(obj.endTime) {
        filter[item.filter] = obj.endTime;
      }
      param = {filter};
      if(param !== {}) {
        this.eventService.eventEmit.emit(listTagName, param);
      }

    }

  }

  getInterger(value) {
    return parseInt(value);
  }

}



